﻿@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="800">
    <MDCard>
        @if (Model.Id == 0)
        {
            <MCardTitle Class="text-h5">
                创建奖品
            </MCardTitle>
        }
        else
        {
            <MCardTitle Class="text-h5">
                编辑奖品
            </MCardTitle>
        }

        @if (Model.Id == 0)
        {
            <MCardText>
                激活码类别的奖品可以先用占位符代替真实的激活码
            </MCardText>
        }
        else
        {
            <MCardText>
                @Model.Name（Id:@Model.Id）
            </MCardText>
        }

        <MCardText>
            <CnGalWebSite.Components.Containers.MasaTitleContainer Icon="fa fa-fw fa fa-fw fa-home" Title="主要信息" Class="mb-4" Collapable Outline OutlineHead>
                <MRow>
                    <MCol Cols="12" Md="6">
                        <CnGalWebSite.Components.Images.UploadButton @bind-ImagePath="@Model.Image" Type="ImageAspectType.None" ShowCropper Text="上传预览图" Class="mb-4"/>
                     </MCol>
                     <MCol Cols="12"></MCol>
                     <MCol Cols="12" Md="6">
                         <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.Type" TValue="LotteryAwardType" Label="类型" />
                     </MCol>
                     <MCol Cols="12" Md="6">
                         <MTextField @bind-Value="Model.Name" Label="名称" />
                     </MCol>
                 </MRow>
             </CnGalWebSite.Components.Containers.MasaTitleContainer>
             <CnGalWebSite.Components.Containers.MasaTitleContainer Icon="fa fa-fw fa-info-circle" Title="基本信息" Class="mb-4" Collapable Outline OutlineHead>
                <MRow>

                    <MCol Cols="12" Md="6">
                        <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.Type" TValue="LotteryAwardType" Label="类型" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="Model.Count" Label="数量" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="Model.Sponsor" Label="赞助商" />
                    </MCol>
                    <MCol Cols="12">
                        <MTextField @bind-Value="Model.Link" Label="链接" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="Model.Integral" Label="附加积分" />
                    </MCol>
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="Model.Priority" Label="优先级" />
                    </MCol>
                </MRow>
            </CnGalWebSite.Components.Containers.MasaTitleContainer>
        </MCardText>



        <MCardActions Class="pb-6 pe-6 ps-6 pt-6">
            <MSpacer />
            <CnGalWebSite.Components.Buttons.MasaButton Text="保存" IsAsync="true" OnClick="OnSave" Icon="mdi-check" Color="success" Rounded />
         </MCardActions>
     </MDCard>
 </MDialog>

 @code {
    [Parameter]
    public EventCallback<EditLotteryAwardModel> OnRefreshTable { get; set; }

    private long Id { get; set; }

    private EditLotteryAwardModel Model { get; set; } = new EditLotteryAwardModel();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool Dialog { get; set; }

    public async Task OnSave()
    {
        var result = Model.Validate();
        if (result.Successful)
        {
            await OnRefreshTable.InvokeAsync(Model);
            Dialog = false;
        }
        else
        {
            await PopupService.ToastErrorAsync("某个地方填写有误哦~", result.Error ?? "某个地方填写有误哦~");
        }
    }


    public async Task Refresh(bool dialog, EditLotteryAwardModel model)
    {
        Dialog = dialog;
        Model = model;

        StateHasChanged();
    }
}
